package co.yixiang.utils;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.gexin.rp.sdk.base.IBatch;
import com.gexin.rp.sdk.base.IIGtPush;
import com.gexin.rp.sdk.base.impl.SingleMessage;
import com.gexin.rp.sdk.base.impl.Target;
import com.gexin.rp.sdk.base.payload.APNPayload;
import com.gexin.rp.sdk.http.IGtPush;
import com.gexin.rp.sdk.template.NotificationTemplate;
import com.gexin.rp.sdk.template.TransmissionTemplate;
import com.gexin.rp.sdk.template.style.Style0;

import java.io.IOException;
import java.util.Date;


/**
 * @Author: DaiALong
 * @Description:
 * @Date: Created in 2020/5/12 20:31
 */
public class PushtoSingle {

    // 详见【概述】-【服务端接入步骤】-【STEP1】说明，获得的应用配置
    private static String appId = "p3ODpXOkl26gtnPvPNJdN2";
    private static String appKey = "8RSdcwaP096cacDB3EzAt4";
    private static String appSecret = "0mrZe5ptkr6woB99UEChE8";
    private static String masterSecret = "sagqEApoG4AfKxjrPhYUI4";


    static String CID = "7b925a091a70d7a862b857c55dfe4be8";
    static String host = "http://sdk.open.api.igexin.com/apiex.htm";


    public static NotificationTemplate getNotificationTemplate() {
        NotificationTemplate template = new NotificationTemplate();
        // 设置APPID与APPKEY
        template.setAppId(appId);
        template.setAppkey(appKey);

        Style0 style = new Style0();
        // 设置通知栏标题与内容
        style.setTitle("请输入通知栏标题");
        style.setText("请输入通知栏内容");
        // 配置通知栏图标
        style.setLogo("icon.png");
        // 配置通知栏网络图标
        style.setLogoUrl("");
        // 设置通知是否响铃，震动，或者可清除
        style.setRing(true);
        style.setVibrate(true);
        style.setClearable(true);
        style.setChannelLevel(3); //设置通知渠道重要性
        template.setStyle(style);

        template.setTransmissionType(2);  // 透传消息接受方式设置，1：立即启动APP，2：客户端收到消息后需要自行处理

        JSONObject jsonObject=new JSONObject();
        jsonObject.put("title","请输入通知栏标题1111");
        jsonObject.put("text","请输入通知栏内容2222");
        System.out.println(jsonObject.toString());

        template.setTransmissionContent(jsonObject.toString());

        return template;
    }


    public static void main(String[] args) throws IOException {
        sendToAlias("充值礼品", "您于" + DateUtil.formatDate(new Date()) + "充值了礼品", CID, 1 , 1L);
    }

    //透传消息
    private static void constructClientTransMsg(String cid, String msg, IBatch batch) throws Exception {

        SingleMessage message = new SingleMessage();
        TransmissionTemplate template = new TransmissionTemplate();
        template.setAppId(appId);
        template.setAppkey(appKey);
        template.setTransmissionContent(msg);
        template.setTransmissionType(2); // 透传消息接受方式设置，1：立即启动APP，2：客户端收到消息后需要自行处理
        template.setAPNInfo(getAPNPayload(msg)); //详见【推送模板说明】iOS通知样式设置

        message.setData(template);
        message.setOffline(true);
        message.setOfflineExpireTime(1000 * 600);
        // 厂商通道下发策略
        message.setStrategyJson("{\"ios\":1,\"st\":4}");

        // 设置推送目标，填入appid和clientId
        Target target = new Target();
        target.setAppId(appId);
        target.setClientId(cid);
        batch.add(message, target);
    }

    private static APNPayload getAPNPayload(String msg) {
        APNPayload payload = new APNPayload();
        //在已有数字基础上加1显示，设置为-1时，在已有数字上减1显示，设置为数字时，显示指定数字
        payload.setAutoBadge("+1");
        payload.setContentAvailable(0);
        //ios 12.0 以上可以使用 Dictionary 类型的 sound
        payload.setSound("default");
        JSONObject jsonObject = JSONObject.parseObject(msg);
        payload.addCustomMsg("type", jsonObject.get("type"));
        payload.addCustomMsg("id", jsonObject.get("id"));
        payload.addCustomMsg("text", jsonObject.get("text"));
        payload.addCustomMsg("subtitle", jsonObject.get("subtitle"));
        //简单模式APNPayload.SimpleMsg
        payload.setAlertMsg(new APNPayload.SimpleAlertMsg(String.valueOf(jsonObject.get("subtitle"))));

        return payload;
    }
    //通知消息
    private static void constructClientLinkMsg(String cid, String title, String text, IBatch batch) throws Exception {

        SingleMessage message = new SingleMessage();
        NotificationTemplate template = new NotificationTemplate();
        // 设置APPID与APPKEY
        template.setAppId(appId);
        template.setAppkey(appKey);

        Style0 style = new Style0();
        // 设置通知栏标题与内容
        style.setTitle(title);
        style.setText(text);
        // 配置通知栏图标
        style.setLogo("icon.png");
        // 配置通知栏网络图标
        style.setLogoUrl("");
        // 设置通知是否响铃，震动，或者可清除
        style.setRing(true);
        style.setVibrate(true);
        style.setClearable(true);
        template.setStyle(style);

        //template.setAPNInfo(getAPNPayload()); //详见【推送模板说明】iOS通知样式设置

        message.setData(template);
        message.setOffline(true);
        message.setOfflineExpireTime(1000 * 600);

        // 设置推送目标，填入appid和clientId
        Target target = new Target();
        target.setAppId(appId);
        target.setClientId(cid);
        batch.add(message, target);
    }


    /**
     * @return
     * @Author:
     * @Description: uni-app推送
     * @Date: 2020/5/14 19:36
     * @param: title    消息标题
     * @param: text     消息内容
     * @param: clientId    用户标识
     * @param: type        1\发货时推送消息 2\购买时推送消息
     */
    public static void sendToAlias(String title, String text, String clientId, Integer type , Long id) {
        IIGtPush push = new IGtPush(host, appKey, masterSecret);
        IBatch batch = push.getBatch();

        try {
            // 透传消息
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("type", type);
            jsonObject.put("subtitle",title);
            jsonObject.put("text",text);
            jsonObject.put("id",id);
            System.out.println(jsonObject.toString());
            constructClientTransMsg(clientId, jsonObject.toString(), batch);
            batch.submit();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


}
